package com.pds.web.company.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.SecurityUtils;
import com.pds.common.utils.StringUtils;
import com.pds.common.utils.bean.BeanUtils;
import com.pds.system.domain.FileUpload;
import com.pds.system.mapper.FileUploadMapper;
import com.pds.utils.FileUtil;
import com.pds.web.area.mapper.AreaMapper;
import com.pds.web.company.domain.PdsCompanyContact;
import com.pds.web.company.domain.PdsCompanyInfo;
import com.pds.web.company.domain.PdsCompanyVO;
import com.pds.web.company.mapper.PdsCompanyVOMapper;
import com.pds.web.company.service.IPdsCompanyVOService;
import com.pds.web.industry.entity.Industry;
import com.pds.web.industry.service.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PdsCompanyVOServiceImpl extends ServiceImpl<PdsCompanyVOMapper, PdsCompanyVO> implements IPdsCompanyVOService {

    @Resource
    private PdsCompanyVOMapper pdsCompanyVOMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private FileUploadMapper fileUploadMapper;
    @Autowired
    IndustryService industryService;

    @Override
    public List<PdsCompanyVO> detailList(PdsCompanyInfo pdsCompanyInfo) {

        List<PdsCompanyVO> pdsCompanyVOList = new ArrayList<>(); //结果集

        //1.查询所有的info：pdsCompanyInfoList
        List<PdsCompanyInfo> pdsCompanyInfoList = pdsCompanyVOMapper.selectCompanyInfo(pdsCompanyInfo);

        //2.遍历pdsCompanyInfoList，根据当前info的id，查询对应的contact
        pdsCompanyInfoList.stream().forEach(companyInfo -> {
            //获取当前机构的id：companyInfoId
            Long companyInfoId = companyInfo.getId();

            //根据companyInfoId查询对应的companyContactList
            List<PdsCompanyContact> companyContactList = pdsCompanyVOMapper.selectCompanyContact(companyInfoId);

            //将CompanyInfo和对应的companyContactList封装进对象pdsCompanyVO
            PdsCompanyVO pdsCompanyVO = new PdsCompanyVO();
            BeanUtils.copyProperties(companyInfo, pdsCompanyVO);
            pdsCompanyVO.setPdsCompanyContacts(companyContactList);

            //将对象pdsCompanyVO插入结果集pdsCompanyVOList
            pdsCompanyVOList.add(pdsCompanyVO);
        });

        return pdsCompanyVOList;
    }

    @Override
    public Long selectCompanyCount(PdsCompanyInfo pdsCompanyInfo) {
        return pdsCompanyVOMapper.selectCompanyCount(pdsCompanyInfo);
    }

    @Override
    public PdsCompanyVO detail() {
        Long organizationId = SecurityUtils.getLoginUser().getUser().getDept().getOrganizationId();
        PdsCompanyInfo companyInfo = pdsCompanyVOMapper.detail(organizationId);

        PdsCompanyVO pdsCompanyVO = new PdsCompanyVO();
        BeanUtils.copyProperties(companyInfo, pdsCompanyVO);
        //查询省市四级文字
        StringBuilder addressCode = new StringBuilder();
        addressCode.append(companyInfo.getProvince()).append(",")
                .append(companyInfo.getCity()).append(",")
                .append(companyInfo.getCounty()).append(",")
                .append(companyInfo.getTownship());
        String addressText = areaMapper.selectAddressByCode(addressCode.toString());
        pdsCompanyVO.setAddressText(addressText);
        //查许可证附件信息
        if(StringUtils.isNotEmpty(companyInfo.getPollutionDischargePermitId())) {
            FileUpload fileUpload = fileUploadMapper.selectFileUploadById(companyInfo.getPollutionDischargePermitId());
            if (fileUpload != null){
                pdsCompanyVO.setPollutionDischargePermit(FileUtil.structureFile(fileUpload));
            }
        }
        //查询行业
        if(StringUtils.isNotEmpty(companyInfo.getIndustry())) {
            Industry industry = industryService.getIndustryById(companyInfo.getIndustry());
            if (industry!=null){
                pdsCompanyVO.setIndustryName(industry.getName());
            }
        }

        return pdsCompanyVO;
    }
}
